package com.threeti.jiayusms.domain.service.legacy;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.stereotype.Service;

import com.threeti.jiayusms.domain.model.defaulttemplate.DefaultCommunicationTemplate;
import com.threeti.jiayusms.domain.model.defaulttemplate.SMSTemplateMapping;
import com.threeti.jiayusms.domain.model.defaulttemplate.VoiceTemplateMapping;

@Service
public class JiaYuDBLegacyUpdateServiceImpl implements JiaYuDBLegacyUpdateService {

  @Autowired SimpleJdbcTemplate template;
  
  @Override
  public void storeSMSMapping(DefaultCommunicationTemplate defaultTemplate, SMSTemplateMapping smsMapping) {
    String mappingCusId = smsMapping.getCustomId();
    if (mappingCusId == null) return;//短信方式编号不能为空
    
    String nodeId = smsMapping.getMsgNode();
    String msgCusId = "";
    if (smsMapping.getMsgTemplate() != null) {
      msgCusId = smsMapping.getMsgTemplate().getCustomId();
    }
    String msgRuleId = "";
    if (smsMapping.getMsgRuleTemplate() != null) {
      msgRuleId = smsMapping.getMsgRuleTemplate().getCustomId();
    }
    String msgTimeId = "";
    if (smsMapping.getMsgTimeTemplate() != null) {
      msgTimeId = smsMapping.getMsgTimeTemplate().getCustomId();
    }
    
    String productId = smsMapping.getMsgProductType();
    String conditionTypeId = smsMapping.getMsgConditionType();
    String conditionId = smsMapping.getMsgCondition();
    int msgEnableFlag = smsMapping.isMsgCommunicationEnabled()?1:0;
    String msgId = defaultTemplate.getCustomId();
    
    String sql = "insert into tx_SPTemp (FSCode, Node, SPTempCode, RuleTempCode, TimeTempCode, " +
    		"ProductInfoCode, ConditionCode, ConditionValueCode, UseFlag, SourceFlag, SourceCode) " +
    		"values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
    //""未启用
    //"M"启用
    template.update(sql, mappingCusId, nodeId, msgCusId, msgRuleId, msgTimeId, productId, conditionTypeId, conditionId, msgEnableFlag, "", msgId);   
  }

  @Override
  public void storeVoiceMapping(DefaultCommunicationTemplate defaultTemplate, VoiceTemplateMapping voiceMapping) {
    String mappingCusId = voiceMapping.getCustomId();
    if (mappingCusId == null) return;//短信方式编号不能为空
    
    String nodeId = voiceMapping.getVoiceNode();
    String voiceCusId = "";
    if (voiceMapping.getVoiceTemplate() != null) {
      voiceCusId = voiceMapping.getVoiceTemplate().getCustomId();
    }
    String voiceRuleId = "";
    if (voiceMapping.getVoiceRuleTemplate() != null) {
      voiceRuleId = voiceMapping.getVoiceRuleTemplate().getCustomId();
    }
    String voiceTimeId = "";
    if (voiceMapping.getVoiceTimeTemplate() != null) {
      voiceTimeId = voiceMapping.getVoiceTimeTemplate().getCustomId();
    }
    String productId = voiceMapping.getVoiceProductType();
    String conditionTypeId = voiceMapping.getVoiceConditionType();
    String conditionId = voiceMapping.getVoiceCondition();
    int voiceEnableFlag = voiceMapping.isVoiceCommunicationEnabled()?1:0;
    String voiceId = defaultTemplate.getCustomId();
    
    String sql = "insert into tx_VITemp (FSCode, Node, VITempCode, RuleTempCode, TimeTempCode, " +
        "ProductInfoCode, ConditionCode, ConditionValueCode, UseFlag, SourceFlag, SourceCode) " +
        "values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
    
    //""未启用
    //"M"启用
    template.update(sql, mappingCusId, nodeId, voiceCusId, voiceRuleId, voiceTimeId, productId, conditionTypeId, conditionId, voiceEnableFlag, "", voiceId); 
  }

  @Override
  public void removeDefaultTemplateBy(String defaultTemplateCustomId) {
    String sqlSMS = "delete from tx_SPTemp where SourceCode = '"+defaultTemplateCustomId+"'";
    String sqlVoice = "delete from tx_VITemp where SourceCode = '"+defaultTemplateCustomId+"'";
    
    template.update(sqlSMS);    
    template.update(sqlVoice);
  }

  @Override
  public void removeSMSMappingBy(String smsMappingCustomId) {
    String sqlSMS = "delete from tx_SPTemp where FSCode = '"+smsMappingCustomId+"'";
    
    template.update(sqlSMS);
  }

  @Override
  public void removeVoiceMappingBy(String voiceMappingCustomId) {
    String sqlSMS = "delete from tx_VITemp where FSCode = '"+voiceMappingCustomId+"'";
    
    template.update(sqlSMS);
  }

  @Override
  public void enableDefaultTemplateBy(String customId) {
    String disableSqlSMS = "update tx_SPTemp set SourceFlag='' where SourceFlag='M'";
    String disableSqlVoice = "update tx_VITemp set SourceFlag='' where SourceFlag='M'";
    
    String enableSqlSMS = "update tx_SPTemp set SourceFlag='M' where SourceCode = '"+customId+"'";
    String enableSqlVoice = "update tx_VITemp set SourceFlag='M' where SourceCode = '"+customId+"'";
    
    //禁用之前的，并启用新的默认模板
    template.update(disableSqlSMS);
    template.update(disableSqlVoice);
    
    template.update(enableSqlSMS);    
    template.update(enableSqlVoice);
  }

}
